<?php

namespace My\Model;

use Utuo\UtuoModel;

class ProductRateModel extends UtuoModel {
	// 评价方
	const REVIEWER_BUYER = 0;
	const REVIEWER_SALLER = 1;
	
	// 评价的状态
	const RATE_STATUS_UNRATE = 0; // 未评价
	const RATE_STATUS_BUYER = 1; // 买家已评
	const RATE_STATUS_SALLER = 2; // 卖家已评
	const RATE_STATUS_BOTH = 3; // 双方已评
	
	/**
	 * 获得新的评价状态
	 * @param unknown $rate_status 当前评价状态
	 * @param unknown $reviewer 评论方
	 * @return string
	 */
	public function getNextRateStatus($rate_status, $reviewer = self::REVIEWER_BUYER) {
		$rateStatus = self::RATE_STATUS_UNRATE;
		if($rate_status == self::RATE_STATUS_UNRATE) {
			$rateStatus = $reviewer===self::REVIEWER_BUYER ? self::RATE_STATUS_BUYER : self::RATE_STATUS_SALLER;
		}elseif($rate_status == self::RATE_STATUS_BUYER or
				$rate_status == self::RATE_STATUS_SALLER) {
			$rateStatus = self::RATE_STATUS_BOTH;
		}
		return $rateStatus; 
	}
	
	// 提交评论
	public function submitRate($order, $rates, $uid, $reviewer = ProductRateModel::REVIEWER_BUYER) {
		if($order['rate_status'] == ProductRateModel::RATE_STATUS_BOTH) return false;
		/*添加评论数据*/
		foreach ($order["orderItems"] as $productId => $productItem) {
			foreach ($productItem["items"] as $item) {
				if(!array_key_exists( $item['orderGoodsId'], $rates) or M("ProductRate")->where(array('order_id' => $order['id'], 'uid' => $uid, 'order_goods_id' => $item['orderGoodsId'], 'reviewer' => $reviewer,))->find()) continue; // 跳过不参与评论的订单商品
				$nextRateStatus = $this->getNextRateStatus($item['rate_status'], $reviewer);
				/*添加买家评论*/
				$addRates = array(
						'order_id' => $order['id'],
						'uid' => $uid,
						'product_id' => $productId,
						'order_goods_id' => $item['orderGoodsId'],
						'content' => $rates[$item['orderGoodsId']]["content"],
						'reviewer' => $reviewer,
						'rate_status' => $nextRateStatus,
						'create_time' => time(),
				);
				M("ProductOrderGoods")->where(array('order_id' => $order['id'], 'id' => $item['orderGoodsId']))->setField("rate_status", $nextRateStatus); // 设置订单商品评论状态
				if(false !==M("ProductRate")->add($addRates)) {
					if($nextRateStatus === ProductRateModel::RATE_STATUS_BOTH) { // 如果卖家已评
						/*设置对方评价状态*/
						$condition = array('order_id'=>$order['id'],
								'product_id' => $productId,
								'order_goods_id' => $item['orderGoodsId'],
								'reviewer' => $reviewer === ProductRateModel::REVIEWER_SALLER ? ProductRateModel::REVIEWER_BUYER : ProductRateModel::REVIEWER_SALLER,);
						M("ProductRate")->where($condition)->setField("rate_status", ProductRateModel::RATE_STATUS_BOTH); // 设置双方已评
					}
				}else{
					return false;
				}
			}
		}
		/*检查是否已评价完*/
		$goodsNumber = M("ProductOrderGoods")->where(array('order_id' => $order['id'],))->count(); // 商品数量
		$currentRateNumber = M("ProductRate")->where(array('order_id' => $order['id'], 'reviewer' => $reviewer,))->count(); // 评论数量
		if($goodsNumber === $currentRateNumber) { // 当前方已完成评论
			$nextRateStatus = $this->getNextRateStatus($order['rate_status'], $reviewer);
			M("ProductOrder")->where("id=%d",$order['id'])->setField("rate_status", $nextRateStatus);
		}
		return true;
	}
}